home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 October / maximum-cd-2009-10.iso / DiscContents / Firefox Setup 3.5.exe / nonlocalized / chrome / toolkit.jar / content / mozapps / plugins / pluginInstallerService.js < prev    next >
Encoding:
Text File  |  2009-06-24  |  10.4 KB  |  308 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Plugin Finder Service.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * IBM Corporation.
  18.  * Portions created by the IBM Corporation are Copyright (C) 2004
  19.  * IBM Corporation. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Doron Rosenberg <doronr@us.ibm.com>
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. const nsIXPIProgressDialog = Components.interfaces.nsIXPIProgressDialog;
  39.  
  40. function getLocalizedError(key)
  41. {
  42.   return document.getElementById("xpinstallStrings").getString(key);
  43. }
  44.  
  45. function binaryToHex(input)
  46. {
  47.   return [('0' + input.charCodeAt(i).toString(16)).slice(-2)
  48.           for (i in input)].join('');
  49. }
  50.  
  51. function verifyHash(aFile, aHash)
  52. {
  53.   try {
  54.     var [, method, hash] = /^([A-Za-z0-9]+):(.*)$/.exec(aHash);
  55.  
  56.     var fis = Components.classes['@mozilla.org/network/file-input-stream;1'].
  57.       createInstance(Components.interfaces.nsIFileInputStream);
  58.     fis.init(aFile, -1, -1, 0);
  59.  
  60.     var hasher = Components.classes['@mozilla.org/security/hash;1'].
  61.       createInstance(Components.interfaces.nsICryptoHash);
  62.     hasher.initWithString(method);
  63.     hasher.updateFromStream(fis, -1);
  64.     dlhash = binaryToHex(hasher.finish(false));
  65.     return dlhash == hash;
  66.   }
  67.   catch (e) {
  68.     Components.utils.reportError(e);
  69.     return false;
  70.   }
  71. }
  72.  
  73. function InstallerObserver(aPlugin)
  74. {
  75.   this._plugin = aPlugin;
  76.   this._init();
  77. }
  78.  
  79. InstallerObserver.prototype = {
  80.   _init: function()
  81.   {
  82.     try {
  83.       var ios = Components.classes["@mozilla.org/network/io-service;1"].
  84.         getService(Components.interfaces.nsIIOService);
  85.       var uri = ios.newURI(this._plugin.InstallerLocation, null, null);
  86.       uri.QueryInterface(Components.interfaces.nsIURL);
  87.  
  88.       // Use a local filename appropriate for the OS
  89.       var leafName = uri.fileName;
  90.       var os = Components.classes["@mozilla.org/xre/app-info;1"]
  91.                          .getService(Components.interfaces.nsIXULRuntime)
  92.                          .OS;
  93.       if (os == "WINNT" && leafName.indexOf(".") < 0)
  94.         leafName += ".exe";
  95.  
  96.       var dirs = Components.classes["@mozilla.org/file/directory_service;1"].
  97.         getService(Components.interfaces.nsIProperties);
  98.  
  99.       var resultFile = dirs.get("TmpD", Components.interfaces.nsIFile);
  100.       resultFile.append(leafName);
  101.       resultFile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE,
  102.                               0770);
  103.  
  104.       var channel = ios.newChannelFromURI(uri);
  105.       this._downloader =
  106.         Components.classes["@mozilla.org/network/downloader;1"].
  107.           createInstance(Components.interfaces.nsIDownloader);
  108.       this._downloader.init(this, resultFile);
  109.       channel.notificationCallbacks = this;
  110.  
  111.       this._fireNotification(nsIXPIProgressDialog.DOWNLOAD_START, null);
  112.  
  113.       channel.asyncOpen(this._downloader, null);
  114.     }
  115.     catch (e) {
  116.       Components.utils.reportError(e);
  117.       this._fireNotification(nsIXPIProgressDialog.INSTALL_DONE,
  118.                              getLocalizedError("error-228"));
  119.       if (resultFile && resultFile.exists())
  120.         resultfile.remove(false);
  121.     }
  122.   },
  123.  
  124.   /**
  125.    * Inform the gPluginInstaller about what's going on.
  126.    */
  127.   _fireNotification: function(aStatus, aErrorMsg)
  128.   {
  129.     gPluginInstaller.pluginInstallationProgress(this._plugin.pid,
  130.                                                 aStatus, aErrorMsg);
  131.  
  132.     if (aStatus == nsIXPIProgressDialog.INSTALL_DONE) {
  133.       --PluginInstallService._installersPending;
  134.       PluginInstallService._fireFinishedNotification();
  135.     }
  136.   },
  137.  
  138.   QueryInterface: function(iid)
  139.   {
  140.     if (iid.equals(Components.interfaces.nsISupports) ||
  141.         iid.equals(Components.interfaces.nsIInterfaceRequestor) ||
  142.         iid.equals(Components.interfaces.nsIDownloadObserver) ||
  143.         iid.equals(Components.interfaces.nsIProgressEventSink))
  144.       return this;
  145.  
  146.     throw Components.results.NS_ERROR_NO_INTERFACE;
  147.   },
  148.  
  149.   getInterface: function(iid)
  150.   {
  151.     if (iid.equals(Components.interfaces.nsIProgressEventSink))
  152.       return this;
  153.  
  154.     return null;
  155.   },
  156.  
  157.   onDownloadComplete: function(downloader, request, ctxt, status, result)
  158.   {
  159.     if (!Components.isSuccessCode(status)) {
  160.       // xpinstall error 228 is "Download Error"
  161.       this._fireNotification(nsIXPIProgressDialog.INSTALL_DONE,
  162.                              getLocalizedError("error-228"));
  163.       result.remove(false);
  164.       return;
  165.     }
  166.  
  167.     this._fireNotification(nsIXPIProgressDialog.DOWNLOAD_DONE);
  168.  
  169.     if (this._plugin.InstallerHash &&
  170.         !verifyHash(result, this._plugin.InstallerHash)) {
  171.       // xpinstall error 261 is "Invalid file hash..."
  172.       this._fireNotification(nsIXPIProgressDialog.INSTALL_DONE,
  173.                              getLocalizedError("error-261"));
  174.       result.remove(false);
  175.       return;
  176.     }
  177.  
  178.     this._fireNotification(nsIXPIProgressDialog.INSTALL_START);
  179.  
  180.     result.QueryInterface(Components.interfaces.nsILocalFile);
  181.     try {
  182.       // Make sure the file is executable
  183.       result.permissions = 0770;
  184.       var process = Components.classes["@mozilla.org/process/util;1"]
  185.                               .createInstance(Components.interfaces.nsIProcess2);
  186.       process.init(result);
  187.       var self = this;
  188.       process.runAsync([], 0, {
  189.         observe: function(subject, topic, data) {
  190.           if (topic != "process-finished") {
  191.             Components.utils.reportError("Failed to launch installer");
  192.             self._fireNotification(nsIXPIProgressDialog.INSTALL_DONE,
  193.                                    getLocalizedError("error-207"));
  194.           }
  195.           else if (process.exitValue != 0) {
  196.             Components.utils.reportError("Installer returned exit code " + process.exitValue);
  197.             self._fireNotification(nsIXPIProgressDialog.INSTALL_DONE,
  198.                                    getLocalizedError("error-203"));
  199.           }
  200.           else {
  201.             self._fireNotification(nsIXPIProgressDialog.INSTALL_DONE, null);
  202.           }
  203.           result.remove(false);
  204.         }
  205.       });
  206.     }
  207.     catch (e) {
  208.       Components.utils.reportError(e);
  209.       this._fireNotification(nsIXPIProgressDialog.INSTALL_DONE,
  210.                              getLocalizedError("error-207"));
  211.       result.remove(false);
  212.     }
  213.   },
  214.  
  215.   onProgress: function(aRequest, aContext, aProgress, aProgressMax)
  216.   {
  217.     gPluginInstaller.pluginInstallationProgressMeter(this._plugin.pid,
  218.                                                      aProgress,
  219.                                                      aProgressMax);
  220.   },
  221.  
  222.   onStatus: function(aRequest, aContext, aStatus, aStatusArg)
  223.   {
  224.     /* pass */
  225.   }
  226. };
  227.  
  228. var PluginInstallService = {
  229.  
  230.   /**
  231.    * Start installation of installers and XPI plugins.
  232.    * @param aInstallerPlugins An array of objects which should have the
  233.    *                          properties "pid", "InstallerLocation",
  234.    *                          and "InstallerHash"
  235.    * @param aXPIPlugins       An array of objects which should have the
  236.    *                          properties "pid", "XPILocation",
  237.    *                          and "XPIHash"
  238.    */
  239.   startPluginInstallation: function (aInstallerPlugins,
  240.                                      aXPIPlugins)
  241.   {
  242.     this._xpiPlugins = aXPIPlugins;
  243.  
  244.     if (this._xpiPlugins.length > 0) {
  245.       this._xpisDone = false;
  246.  
  247.       var xpiManager = Components.classes["@mozilla.org/xpinstall/install-manager;1"]
  248.                                  .createInstance(Components.interfaces.nsIXPInstallManager);
  249.       xpiManager.initManagerWithHashes(
  250.         [plugin.XPILocation for each (plugin in this._xpiPlugins)],
  251.         [plugin.XPIHash for each (plugin in this._xpiPlugins)],
  252.         this._xpiPlugins.length, this);
  253.     }
  254.     else {
  255.       this._xpisDone = true;
  256.     }
  257.  
  258.     // InstallerObserver may finish immediately so we must initialise the
  259.     // installers after setting the number of installers and xpis pending
  260.     this._installersPending = aInstallerPlugins.length;
  261.     this._installerPlugins = [new InstallerObserver(plugin)
  262.                               for each (plugin in aInstallerPlugins)];
  263.   },
  264.  
  265.   _fireFinishedNotification: function()
  266.   {
  267.     if (this._installersPending == 0 && this._xpisDone)
  268.       gPluginInstaller.
  269.         pluginInstallationProgress(null, nsIXPIProgressDialog.DIALOG_CLOSE,
  270.                                    null);
  271.   },
  272.  
  273.   // XPI progress listener stuff
  274.   onStateChange: function (aIndex, aState, aValue)
  275.   {
  276.     // get the pid to return to the wizard
  277.     var pid = this._xpiPlugins[aIndex].pid;
  278.     var errorMsg;
  279.  
  280.     if (aState == nsIXPIProgressDialog.INSTALL_DONE) {
  281.       if (aValue != 0) {
  282.         var xpinstallStrings = document.getElementById("xpinstallStrings");
  283.         try {
  284.           errorMsg = xpinstallStrings.getString("error" + aValue);
  285.         }
  286.         catch (e) {
  287.           errorMsg = xpinstallStrings.getFormattedString("unknown.error", [aValue]);
  288.         }
  289.       }
  290.     }
  291.  
  292.     if (aState == nsIXPIProgressDialog.DIALOG_CLOSE) {
  293.       this._xpisDone = true;
  294.       this._fireFinishedNotification();
  295.     }
  296.     else {
  297.       gPluginInstaller.pluginInstallationProgress(pid, aState, errorMsg);
  298.     }
  299.   },
  300.  
  301.   onProgress: function (aIndex, aValue, aMaxValue)
  302.   {
  303.     // get the pid to return to the wizard
  304.     var pid = this._xpiPlugins[aIndex].pid;
  305.     gPluginInstaller.pluginInstallationProgressMeter(pid, aValue, aMaxValue);
  306.   }
  307. }
  308.